package com.wuhao.shootclient.base;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.view.View;
import android.view.WindowManager;

import com.wuhao.shootclient.ui.widget.LoadingDialog;
import com.wuhao.shootclient.util.eventbus.AppEventBus;
import com.wuhao.shootclient.util.eventbus.EventCenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * Create by hao on 2019-10-29
 */
public abstract class BaseActivity extends AppCompatActivity {
    private LoadingDialog loadingDialog;
    public CompositeDisposable newRx = new CompositeDisposable();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getViewBind() != null) {
            setContentView(getViewBind());
        }else {
            setContentView(getContentView());
        }
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        ButterKnife.bind(this);
        initWidget();
        initData();
    }

    protected void initEvent() {
        if (!AppEventBus.getInstance().isRegistered(this)) {
            AppEventBus.getInstance().register(this);
        }
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCenter center) {
        if (null != center) {
            switch (center.getEventCode()) {
               /* case EventBusCode.LOADING_DIALOG:
                    showLoadingDialog((boolean) center.getData());
                    break;*/
                default:
                    onEventComing(center);
                    break;
            }

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean isAllGranted = true;
        for (int grantResult : grantResults) {
            isAllGranted &= (grantResult == PackageManager.PERMISSION_GRANTED);
        }
        onRequestPermissionResult(requestCode, isAllGranted);
    }

    protected void onRequestPermissionResult(int requestCode, boolean isAllGranted) {

    }

    protected boolean checkPermissions(String[] neededPermissions) {
        if (neededPermissions == null || neededPermissions.length == 0) {
            return true;
        }
        boolean allGranted = true;
        for (String neededPermission : neededPermissions) {
            allGranted &= ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED;
        }
        return allGranted;
    }

    /**
     * EventBus接收消息
     *
     * @param center 获取事件总线信息
     */
    protected void onEventComing(EventCenter center) {
    }


    protected void initWidget() {
    }

    protected void initData() {
    }

    protected abstract int getContentView();

    protected abstract View getViewBind();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppEventBus.getInstance().unregister(this);
        newRx.clear();
    }

    //字体大小
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
        {
            getResources();
        }
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

    public void showLoadingDialog(boolean isShowing) {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog.getInstance();
        }
        if (isShowing) {
            loadingDialog.show(this);
        } else {
            loadingDialog.disMiss();
        }
    }
}
